package com.xywy.bloodandroid.act;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xywy.bloodandroid.MyApplication;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.dao.BaseDao;
import com.xywy.bloodandroid.dao.BloodPressureDataDao;
import com.xywy.bloodandroid.util.Constants;

/**
 * 账户
 */
public class AccountActivity extends BaseActivity {

	private LinearLayout ll_user_name; // username click
	private LinearLayout ll_pass_word; // password click
	private Button btn_exit_account; // exit click
	private TextView middle_title; // titlt
	private RelativeLayout left_img; // left click
	private TextView tv_phonenum;
	private BloodPressureDataDao bloodDao; // greenDao
	private SharedPreferences sp;
	private Dialog dialog;
	private Button bt_cancle;
	private Button bt_comfirm;

	@Override
	protected void initView(Bundle savedInstanceState) {

		setContentView(R.layout.activity_account);

		ll_user_name = (LinearLayout) findViewById(R.id.ll_user_name);
		ll_pass_word = (LinearLayout) findViewById(R.id.ll_pass_word);
		btn_exit_account = (Button) findViewById(R.id.btn_exit_account);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("账户");
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		tv_phonenum = (TextView) findViewById(R.id.tv_phonenum);
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		ll_user_name.setOnClickListener(this);
		ll_pass_word.setOnClickListener(this);
		btn_exit_account.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		tv_phonenum.setText(User.getInstance(this).getPhonenum());
		initDao();
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			AccountActivity.this.finish();
			break;
		case R.id.ll_user_name:
			enterActivity(PhoneNumberActivity.class);
			break;
		case R.id.ll_pass_word:
			enterActivity(ChangeNewPasswordActivity.class);
			break;
		case R.id.btn_exit_account:
			bloodDao.deleteAll();
			sp = getSharedPreferences("config", Context.MODE_PRIVATE);
			sp.edit().clear().commit();
			SharedPreferences sp_user = getSharedPreferences("xywy_blood_info",
					Activity.MODE_PRIVATE);
			sp_user.edit().clear().commit();
			
			showToast("退出对话框");
			exitLogin();
			break;
		case R.id.bt_comfirm:
			// dialog.dismiss();
			SharedPreferences sp = Constants.getSp(this);
			Editor editor = sp.edit();
			editor.putBoolean("auto_login", true).commit();
			 MyApplication.getInstance().exit();
			SharedPreferences sp1 = context.getSharedPreferences(
					"xywy_blood_info", Activity.MODE_PRIVATE);
			sp1.edit().clear();
			enterActivity(LoginAct.class);
			break;
		case R.id.bt_cancle:
			dialog.dismiss();
			break;

		default:
			break;
		}
	}

	/**
	 * 验证原密码的对话框
	 */
	private void exitLogin() {

		dialog = new Dialog(this);
		dialog.setTitle("退出登录");
		dialog.setContentView(R.layout.dialog_exit);
		bt_cancle = (Button) dialog.findViewById(R.id.bt_cancle);
		bt_comfirm = (Button) dialog.findViewById(R.id.bt_comfirm);
		bt_cancle.setOnClickListener(this);
		bt_comfirm.setOnClickListener(this);
		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();

	}

	private void initDao() {
		BaseDao basedao = BaseDao.getInstance(this);
		bloodDao = basedao.getBloodPressureDataDao();
	}

}
